package com.atguigu.member.members.service.impl;

import com.atguigu.member.members.entity.UmsMemberLevelEntity;
import com.atguigu.member.members.service.UmsMemberLevelService;
import com.atguigu.member.members.to.AutoLoginResponse;
import com.atguigu.member.members.to.UserLoginVo;
import com.atguigu.member.members.to.UserRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.member.members.dao.UmsMemberDao;
import com.atguigu.member.members.entity.UmsMemberEntity;
import com.atguigu.member.members.service.UmsMemberService;


@Service("umsMemberService")
public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberDao, UmsMemberEntity> implements UmsMemberService {


    @Autowired
    private UmsMemberLevelService umsMemberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UmsMemberEntity> page = this.page(
                new Query<UmsMemberEntity>().getPage(params),
                new QueryWrapper<UmsMemberEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 完成用户的注册
     *
     * @param userRegistVo
     */
    public void ret(UserRegistVo userRegistVo) {
        UmsMemberEntity umsMemberEntity = new UmsMemberEntity();

        //设置用户名
        umsMemberEntity.setUsername(userRegistVo.getUserName());
        //会员等级id
        UmsMemberLevelEntity status = umsMemberLevelService.getOne(new QueryWrapper<UmsMemberLevelEntity>().eq("default_status", 1));
        umsMemberEntity.setLevelId(status.getId());
        //密码加密--我们这里使用
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(userRegistVo.getPassword());
        umsMemberEntity.setPassword(encode);
        //手机号
        umsMemberEntity.setMobile(userRegistVo.getPhone());
        //注册时间
        umsMemberEntity.setCreateTime(new Date());

        //调用方法保存注册信息
        this.save(umsMemberEntity);

    }

    /**
     * 用户登录
     *
     * @param userLoginVo
     * @return
     */
    public UmsMemberEntity login(UserLoginVo userLoginVo) {
        //根据用户名或者手机号查询是否存在该用户
        UmsMemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<UmsMemberEntity>().eq("mobile", userLoginVo.getLoginacct()).or().eq("username", userLoginVo.getLoginacct()));
        if (memberEntity != null) {
            //验证用户的密码
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(userLoginVo.getPassword(), memberEntity.getPassword());
            if (matches) {
                //说明密码正确
                return memberEntity;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * 社交登录注册
     *
     * @param autoLoginResponse
     */
    public void autoLogin(AutoLoginResponse autoLoginResponse) {
        //根据social_uid查询是否存在社交登录用户
        UmsMemberEntity umsMemberEntity = this.baseMapper.selectOne(new QueryWrapper<UmsMemberEntity>().eq("social_uid", autoLoginResponse.getUid()));
        UmsMemberEntity entity = new UmsMemberEntity();
        if (umsMemberEntity != null) {

            entity.setId(umsMemberEntity.getId());
            entity.setAccessToken(autoLoginResponse.getAccessToken());
            entity.setExpiresIn(autoLoginResponse.getExpiresIn());
            //根据id更新数据
            this.updateById(entity);
        } else {
            //新增数据到表中
            entity.setLevelId(umsMemberLevelService.getOne(new QueryWrapper<UmsMemberLevelEntity>().eq("default_status", 1)).getId());
            entity.setCreateTime(new Date());
            entity.setAccessToken(autoLoginResponse.getAccessToken());
            entity.setExpiresIn(autoLoginResponse.getExpiresIn());
            entity.setUsername(autoLoginResponse.getUserName());
            entity.setSocialUid(autoLoginResponse.getUid());
            this.save(entity);
        }
    }

}